#!/bin/bash
#2021-12-12 cnetos7+
Core=`cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l`
Geturl="https://mirror.cnop.net"
echo "Cpu core number $Core"
echo -e "\033[32m Enter a nginx path( Default /usr/local/nginx ):   \033[0m"
read Dir
if [ -z "$Dir" ]; then
  echo -e "\033[32m  Using the default path /usr/local/nginx :\033[0m"
  Dir=/usr/local/nginx
  echo $Dir
else
  echo -e "\033[32m Your input $Path :\033[0m"
  echo $Dir
fi

if [ -e $Dir ]; then
#Folder name exists in system
echo -e "\033[32m Ready to Install.   \033[0m"
else
#Folder name no exists in system
echo -e "\033[32m Create $Dir   \033[0m"
mkdir $Dir
fi

#nginx_version
echo " " && echo -e "\033[32m Please enter a nginx version using Numbers instead: \033[0m"
echo -e "\033[32m 1.tengine-2.2.1   \033[0m"
echo -e "\033[32m 2.tengine-2.3.2    \033[0m"
echo -e "\033[32m 3.tengine-2.3.3  (Default)  \033[0m"
read Vname
case $Vname in
1)
    echo tengine-2.2.1
	Vname=tengine-2.2.1
    ;;
2)
    echo tengine-2.3.2 	
	Vname=tengine-2.3.2
    ;;
3|'')
    echo tengine-2.3.3 	
	Vname=tengine-2.3.3
    ;;
esac
echo -e "\033[32m Install lib: \033[0m"
yum -y install  epel-release jemalloc pcre* openssl* unzip wget zip

echo -e "\033[32m Install Yunsuo ? ( y or n ) \033[0m"
read Yunsuo
if [ "$Yunsuo" = "y" ]; then
echo -e "\033[32m Install Yunsuo. \033[0m"
cd /home
Yunsuo_mode=`wget $Geturl/security/yunsuo/yunsuo-nginx-plugin-master.zip && unzip yunsuo-nginx-plugin-master.zip`
Yunsuo_plugin="--add-module=/home/nginx-plugin-master"
else
echo -e "\033[32m Not install Yunsuo. \033[0m"
Yunsuo_mode=""
Yunsuo_plugin=""
fi

echo -e "\033[32m Install geoip2 ? ( y or n ) \033[0m"
read Geoip2
if [ "$Geoip2" = "y" ]; then
echo -e "\033[32m Install geoip2. \033[0m"
yum install libmaxminddb-devel -y
cd /home && wget $Geturl/web/module/ngx_http_geoip2_module-master.zip && unzip ngx_http_geoip2_module-master.zip
Geoip2_mode="--add-module=/home/ngx_http_geoip2_module-master"
Geoip2_plugin="--add-module=/home/ngx_http_geoip2_module-master"
Geoipdir="/usr/share/GeoIP"
mkdir $Geoipdir
cd $Geoipdir && wget $Geturl/web/module/Geoip2.zip && unzip Geoip2.zip && rm -rf Geoip2.zip
else
echo -e "\033[32m Not install geoip2. \033[0m"
Geoip2_mode=""
Geoip2_plugin=""
fi

Version=`cat /etc/redhat-release |grep "CentOS Linux release 7"`
#Firewalld=`ps -ef|grep "/usr/sbin/firewalld"`

useradd www
echo -e "\033[32m Download $Vname.tar.gz: \033[0m"
cd /home  && wget $Geturl/web/tengine/$Vname.tar.gz 
tar zxvf $Vname.tar.gz 
$Yunsuo_mode
cd $Vname && echo "$Vname"

if [ "$Vname" = "tengine-2.2.1" ]; then
./configure --prefix=$Dir --with-http_ssl_module --with-http_concat_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_upstream_check_module  $Yunsuo_plugin $Geoip2_plugin
else
./configure --user=www --group=www  --prefix=$Dir --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --add-module=./modules/ngx_http_upstream_vnswrr_module --add-module=./modules/ngx_http_concat_module $Yunsuo_plugin $Geoip2_plugin
fi

make && make install
cp $Dir/sbin/nginx   /usr/local/bin/ && chmod +x /usr/local/bin/nginx && cp $Dir/sbin/nginx /usr/sbin/ && chmod +x /usr/sbin/nginx
cd /etc/init.d && wget $Geturl/web/tengine/tengine && mv tengine nginx 
sed -i "s@/home/jjzb/vol1/nginx@$Dir@g" nginx && chmod +x nginx

if [ ! -n "$Version" ]; then 
#for centos 7-
#echo -e "\033[32m  Seting firewall for centos ... \033[0m"
#iptables -I INPUT -p tcp -m multiport --dports 80 -j ACCEPT
#service iptables save 
#service iptables restart  
echo -e "\033[32m starting nginx... \033[0m"
service nginx start
echo "service nginx start" >>/etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

else  
#for centos 7
#if [ ! -n "$Firewalld" ]; then 
#echo "Skip firewall."
#else
#firewall-cmd --zone=public --add-port=80/tcp --permanent
#echo -e "\033[32m Restart firewall ... \033[0m"
#systemctl restart firewalld.service
#fi
echo -e "\033[32m starting nginx... \033[0m"
chkconfig --add nginx && systemctl daemon-reload && /sbin/chkconfig  nginx on && systemctl start nginx.service
fi

sleep 3
Url=http://127.0.0.1
Code=`curl -I -m 10 -o /dev/null -s -w %{http_code}  $Url`
if [ $Code = "200" ]; then
#url visit ...
ps -ef|grep nginx
echo -e "\033[32m install  is succeed.\033[0m"
else
echo -e "\033[31m Error : Do not start the nginx. \033[0m"
fi